package cn.itrip.order.service;

import cn.itrip.dao.*;
import cn.itrip.dto.Dto;
import cn.itrip.pojo.*;
import cn.itrip.utlis.*;
import cn.itrip.vo.OrderSelectCondition;
import cn.itrip.vo.order.*;
import cn.itrip.vo.store.StoreVO;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "orderService")
public class OrderServiceImpl implements OrderService {

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private ItripHotelMapper itripHotelMapper;
    @Resource
    private ItripHotelRoomMapper itripHotelRoomMapper;
    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Resource
    private ItripUserLinkUserMapper itripUserLinkUserMapper;

    /**
     * token是否存在
     * @param agent
     * @param token
     * @return
     * @throws Exception
     */
    @Override
    public boolean exitToken(String agent, String token) throws Exception {
        String value=redisUtils.getValue(token);
        if (value==null){
            throw new Exception("用户已过期，请重新认证");
        }
        return true;
    }

    /**
     * 根据订单id查询个人订单的详细信息
     * @param orderId  (VO)
     * @return Dto
     * @throws Exception
     */
    @Override
    public ItripPersonalHotelOrderVO findByOrderId(long orderId) throws Exception {
        ItripHotelOrder itripHotelOrder=itripHotelOrderMapper.findByOrderId(orderId);
        ItripPersonalHotelOrderVO itripPersonalHotelOrderVO=new ItripPersonalHotelOrderVO();
        itripPersonalHotelOrderVO.setNoticePhone(itripHotelOrder.getNoticephone());
        itripPersonalHotelOrderVO.setOrderNo(itripHotelOrder.getOrderno());
        itripPersonalHotelOrderVO.setRoomPayType(itripHotelOrder.getPaytype());
        itripPersonalHotelOrderVO.setCreationDate(itripHotelOrder.getCreationdate());
        itripPersonalHotelOrderVO.setPayAmount(itripHotelOrder.getPayamount());
        itripPersonalHotelOrderVO.setOrderStatus(itripHotelOrder.getOrderstatus());
        itripPersonalHotelOrderVO.setId(itripHotelOrder.getId());
        itripPersonalHotelOrderVO.setBookType(itripHotelOrder.getBooktype());
        return  itripPersonalHotelOrderVO;
    }

    /**
     * 根据订单id  获取订单信息
     * @param orderId
     * @return
     */
    @Override
    public ItripHotelOrder findByOrderId1(long orderId) {
        return itripHotelOrderMapper.findByOrderId(orderId);
    }

    /**
     * 根据条件查询订单的总数量
     * @param orderSelectCondition
     * @return
     * @throws Exception
     */
    @Override
    public int findOrderByQuery(OrderSelectCondition orderSelectCondition) throws Exception {
        return itripHotelOrderMapper.selectTatol(orderSelectCondition);
    }

    /**
     * 条件查询+分页
     * @param orderSelectCondition
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public Page<ItripListHotelOrderVO> findOrderBypage(OrderSelectCondition orderSelectCondition,Integer pageNo,Integer pageSize) throws Exception {

        //调 mapper  返回一个类型为 iteripHotelOrder类型的集合
        List<ItripHotelOrder> list=new ArrayList();
        //将数据库查取出来的集合  遍历遍历分装在要返回的VO对象中
        ItripListHotelOrderVO itripListHotelOrderVO=null;
        List<ItripListHotelOrderVO> orderVOS=new ArrayList<>();
        //page  页面拿到的最终对象
        Page<ItripListHotelOrderVO> page=new Page<ItripListHotelOrderVO>();
        //根据条件查询总条数
        int total=itripHotelOrderMapper.selectTatol(orderSelectCondition);
        PageQuery pageQuery=new PageQuery(pageNo,pageSize,total);
        orderSelectCondition.setBeginPos(pageQuery.getBeginPos());
        orderSelectCondition.setPageSize(pageQuery.getPageSize());
        list=itripHotelOrderMapper.findByPageQuery(orderSelectCondition);
        for (ItripHotelOrder itripHotelOrder:list){
            itripListHotelOrderVO=new ItripListHotelOrderVO();
            itripListHotelOrderVO.setId(itripHotelOrder.getId());
            itripListHotelOrderVO.setHotelId(itripHotelOrder.getHotelid());
            itripListHotelOrderVO.setHotelName(itripHotelOrder.getHotelname());
            itripListHotelOrderVO.setOrderNo(itripHotelOrder.getOrderno());
            itripListHotelOrderVO.setOrderType(itripHotelOrder.getOrdertype());
            itripListHotelOrderVO.setLinkUserName(itripHotelOrder.getLinkusername());
            itripListHotelOrderVO.setCreationDate(itripHotelOrder.getCreationdate());
            itripListHotelOrderVO.setCheckInDate(itripHotelOrder.getCheckindate());
            itripListHotelOrderVO.setPayAmount(itripHotelOrder.getPayamount());
            itripListHotelOrderVO.setOrderStatus(itripHotelOrder.getOrderstatus());
            orderVOS.add(itripListHotelOrderVO);
        }
        //封装页面需要的page 对象里的所有信息
        page.setRows(orderVOS);
        page.setBeginPos(pageQuery.getBeginPos());
        page.setTotal(total);
        page.setCurPage(pageQuery.getCurPage());
        page.setPageSize(pageQuery.getPageSize());
        page.setPageCount(pageQuery.getPageCount());
        return page;
    }

    /**
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public List findLinkUserByUserId(long userId) throws Exception {

        return itripUserLinkUserMapper.findLinkUserByUserId(userId);
    }

    /**
     *查找房屋信息
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public ItripPersonalOrderRoomVO getItripHotelOrderRoomInfoById(long orderId) throws Exception {
        return  itripHotelOrderMapper.getItripHotelOrderRoomInfoById(orderId);
    }

    /**
     * 根据订单id查询订单详情
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public ItripModifyHotelOrderVO findByIdForOrder(long orderId) throws Exception {
        return itripHotelOrderMapper.findByIdForOrder(orderId);
    }

    /**
     * 根据订单id删除订单
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByOrderId(long orderId) throws Exception {
            return itripHotelOrderMapper.deleteByPrimaryKey(orderId);
    }

    /**
     * 查找酒店信息
     * @param id
     * @return
     */
    @Override
    public ItripHotel findByHotelId(Long id) {
        return itripHotelMapper.selectByPrimaryKey(id);
    }

    /**
     * 查找房间信息
     * @param id
     * @return
     */
    @Override
    public ItripHotelRoom findByRoomId(Long id) {
        return itripHotelRoomMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询库存
     * @param validateRoomStoreVO
     * @return
     */
    @Override
    public List<StoreVO> findByQuery(ValidateRoomStoreVO validateRoomStoreVO) {
        itripHotelTempStoreMapper.selectByQuery(validateRoomStoreVO);
        return itripHotelTempStoreMapper.updateRoomStore(validateRoomStoreVO);
    }

    /**添加订单信息
     *
     * @param itripHotelOrder
     * @return
     * @throws Exception
     */
    @Override
    public int addOrderInfo(ItripHotelOrder itripHotelOrder) throws Exception {
        return itripHotelOrderMapper.insertSelective(itripHotelOrder);
    }

    /**
     * 根据id查找旅客
     * @param id
     * @return
     */
    @Override
    public ItripUserLinkUser findByUserId(Long id) {
        return itripUserLinkUserMapper.selectByPrimaryKey(id);
    }

    /**
     * 刷新成取消
     * @return
     * @throws Exception
     */
    @Override
    public boolean flushCancleOrderStatus() throws Exception {
        return  itripHotelOrderMapper.flushCancleOrderStatus()>0;
    }

    /**
     * 刷新成待评价
     * @return
     * @throws Exception
     */
    @Override
    public boolean flushCommentlOrderStatus() throws Exception {
        return  itripHotelOrderMapper.flushCommentlOrderStatus()>0;
    }
}
